home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / python / xpcom / server / loader.py < prev    next >
Encoding:
Python Source  |  2006-01-20  |  4.8 KB  |  116 lines

  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. #
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS IS" basis,
  10. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. # for the specific language governing rights and limitations under the
  12. # License.
  13. #
  14. # The Original Code is the Python XPCOM language bindings.
  15. #
  16. # The Initial Developer of the Original Code is
  17. # Activestate Tool Corp.
  18. # Portions created by the Initial Developer are Copyright (C) 2000
  19. # the Initial Developer. All Rights Reserved.
  20. #
  21. # Contributor(s):
  22. #    Mark Hammond <MarkH@ActiveState.com>
  23. #
  24. # Alternatively, the contents of this file may be used under the terms of
  25. # either the GNU General Public License Version 2 or later (the "GPL"), or
  26. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. # in which case the provisions of the GPL or the LGPL are applicable instead
  28. # of those above. If you wish to allow use of your version of this file only
  29. # under the terms of either the GPL or the LGPL, and not to allow others to
  30. # use your version of this file under the terms of the MPL, indicate your
  31. # decision by deleting the provisions above and replace them with the notice
  32. # and other provisions required by the GPL or the LGPL. If you do not delete
  33. # the provisions above, a recipient may use your version of this file under
  34. # the terms of any one of the MPL, the GPL or the LGPL.
  35. #
  36. # ***** END LICENSE BLOCK *****
  37.  
  38. import xpcom
  39. from xpcom import components, nsError
  40. import xpcom.shutdown
  41.  
  42. import module
  43.  
  44. import os, types
  45.  
  46. def _has_good_attr(object, attr):
  47.     # Actually allows "None" to be specified to disable inherited attributes.
  48.     return getattr(object, attr, None) is not None
  49.  
  50. def FindCOMComponents(py_module):
  51.     # For now, just run over all classes looking for likely candidates.
  52.     comps = []
  53.     for name, object in py_module.__dict__.items():
  54.         if type(object)==types.ClassType and \
  55.            _has_good_attr(object, "_com_interfaces_") and \
  56.            _has_good_attr(object, "_reg_clsid_") and \
  57.            _has_good_attr(object, "_reg_contractid_"):
  58.             comps.append(object)
  59.     return comps
  60.  
  61. def register_self(klass, compMgr, location, registryLocation, componentType):
  62.     pcl = ModuleLoader
  63.     from xpcom import _xpcom
  64.     svc = _xpcom.GetServiceManager().getServiceByContractID("@mozilla.org/categorymanager;1", components.interfaces.nsICategoryManager)
  65.     # The category 'module-loader' is special - the component manager uses it
  66.     # to create the nsIModuleLoader for a given component type.
  67.     svc.addCategoryEntry("module-loader", pcl._reg_component_type_, pcl._reg_contractid_, 1, 1)
  68.  
  69. # The Python module loader.  Called by the component manager when it finds
  70. # a component of type self._reg_component_type_.  Responsible for returning
  71. # an nsIModule for the file.
  72. class ModuleLoader:
  73.     _com_interfaces_ = components.interfaces.nsIModuleLoader
  74.     _reg_clsid_ = "{945BFDA9-0226-485e-8AE3-9A2F68F6116A}" # Never copy these!
  75.     _reg_contractid_ = "@mozilla.org/module-loader/python;1"
  76.     _reg_desc_ = "Python module loader"
  77.     # Optional function which performs additional special registration
  78.     # Appears that no special unregistration is needed for ModuleLoaders, hence no unregister function.
  79.     _reg_registrar_ = (register_self,None)
  80.     # Custom attributes for ModuleLoader registration.
  81.     _reg_component_type_ = "application/x-python"
  82.  
  83.     def __init__(self):
  84.         self.com_modules = {} # Keyed by module's FQN as obtained from nsIFile.path
  85.         self.moduleFactory = module.Module
  86.         xpcom.shutdown.register(self._on_shutdown)
  87.  
  88.     def _on_shutdown(self):
  89.         self.com_modules.clear()
  90.  
  91.     def loadModule(self, aFile):
  92.         return self._getCOMModuleForLocation(aFile)
  93.  
  94.     def _getCOMModuleForLocation(self, componentFile):
  95.         fqn = componentFile.path
  96.         if not fqn.endswith(".py"):
  97.             raise xpcom.ServerException(nsError.NS_ERROR_INVALID_ARG)
  98.         mod = self.com_modules.get(fqn)
  99.         if mod is not None:
  100.             return mod
  101.         import ihooks, sys
  102.         base_name = os.path.splitext(os.path.basename(fqn))[0]
  103.         loader = ihooks.ModuleLoader()
  104.  
  105.         module_name_in_sys = "component:%s" % (base_name,)
  106.         stuff = loader.find_module(base_name, [componentFile.parent.path])
  107.         assert stuff is not None, "Couldn't find the module '%s'" % (base_name,)
  108.         py_mod = loader.load_module( module_name_in_sys, stuff )
  109.  
  110.         # Make and remember the COM module.
  111.         comps = FindCOMComponents(py_mod)
  112.         mod = self.moduleFactory(comps)
  113.         
  114.         self.com_modules[fqn] = mod
  115.         return mod
  116.